package com.shms.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shms.bean.GroupOfHome;
import com.shms.constant.QueryConstant;
import com.shms.entity.ShHome;
import com.shms.service.ShHomeService;

public class HomeAction  extends ActionSupport {
	private static final long serialVersionUID = -7106008904232245508L;
	private ShHomeService shHomeService;
	private GroupOfHome groupOfHome;
	public ShHome shHome;
	private Integer[] nos;
	public String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Integer[] getNos() {
		return nos;
	}
	public void setNos(Integer[] nos) {
		this.nos = nos;
	}
	
	public String addHome() {
		//
		ActionContext.getContext().put("buildname", 0);
		return SUCCESS;
	}
	
	public String saveHome() {
		boolean rtn = shHomeService.saveHome(shHome);
		if (rtn == false) {
			message = "保存失败";
		} else {
			message = "保存成功";
		}
		ActionContext.getContext().put("buildname", shHome.getBid());
		return SUCCESS;
	}
	
	public String queryByHomeId() {
		//获取要查询的单条记录id
		long id = Long.parseLong(ServletActionContext.getRequest().getParameter("no"));
		ShHome shHomeParams = new ShHome();
		shHomeParams.setHid(id);
		shHome = shHomeService.queryById(shHomeParams);
		//
		ActionContext.getContext().put("buildname", shHome.getBid());
		return SUCCESS;
	}
	
	/**
	 * 根据分页信息，查询宿舍列表
	 * @return 
	 */
	public String queryHome() {
		//获取传来的当前页数
		Integer currentPage = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
		//获取传来的上一页或下一页标志
		String type = ServletActionContext.getRequest().getParameter("param");
		//构造查询动态信息的分页信息输入参数
		GroupOfHome HomeOfParams = new GroupOfHome();
		HomeOfParams.setCurrentPage(currentPage);
		HomeOfParams.setType(type);
		HomeOfParams.setPageSize(QueryConstant.F_Pagesize);
		//查询
		groupOfHome = shHomeService.queryHome(HomeOfParams);
		if (groupOfHome.getClass() == null)
			return INPUT;
		else 
			return SUCCESS;
	}
	/**
	 * 批量删除宿舍楼信息
	 * @return 
	 */
	public String deleteHomes() {
		//获取要删除的单条记录id
		if (ServletActionContext.getRequest().getParameter("no") != null ) {
			Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("no"));
			ShHome sh=new ShHome();
			sh.setHid(Long.parseLong(ServletActionContext.getRequest().getParameter("no")));
			if(shHomeService.queryPointsByHId(sh).size()>0){
				message="该宿舍已经存在分数记录，不允许删除";
				//获取传来的当前页数
				Integer currentPage =groupOfHome.getCurrentPage()-1;
				//获取传来的上一页或下一页标志
				String type = groupOfHome.getType();
				//构造查询动态信息的分页信息输入参数
				GroupOfHome groupOfHomeParams = new GroupOfHome();
				groupOfHomeParams.setCurrentPage(currentPage);
				groupOfHomeParams.setType(type);
				groupOfHomeParams.setPageSize(QueryConstant.F_Pagesize);
				//查询
				groupOfHome = shHomeService.queryHome(groupOfHomeParams);
				return INPUT;
			}
			Integer[] ids = {id};
			nos = ids;
		}
		//删除成功重新查询数据，并返回
		if(shHomeService.batchDelete(nos)){
			//获取传来的当前页数
			Integer currentPage =groupOfHome.getCurrentPage()-1;
			//获取传来的上一页或下一页标志
			String type = groupOfHome.getType();
			//构造查询动态信息的分页信息输入参数
			GroupOfHome groupOfHomeParams = new GroupOfHome();
			groupOfHomeParams.setCurrentPage(currentPage);
			groupOfHomeParams.setType(type);
			groupOfHomeParams.setPageSize(QueryConstant.F_Pagesize);
			//查询
			groupOfHome = shHomeService.queryHome(groupOfHomeParams);
			return SUCCESS;
		}else{
			message = "删除宿舍楼信息失败";
			return INPUT;
		}
	}
	public ShHomeService getShHomeService() {
		return shHomeService;
	}
	public void setShHomeService(ShHomeService shHomeService) {
		this.shHomeService = shHomeService;
	}
	public GroupOfHome getGroupOfHome() {
		return groupOfHome;
	}
	public void setGroupOfHome(GroupOfHome groupOfHome) {
		this.groupOfHome = groupOfHome;
	}
	public ShHome getShHome() {
		return shHome;
	}
	public void setShHome(ShHome shHome) {
		this.shHome = shHome;
	}

}
